package com.smfx.yxt.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smfx.yxt.common.AppUser;
import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.MenuConst;
import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.FileType;
import com.smfx.yxt.common.enums.RelationType;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.common.enums.UserRole;
import com.smfx.yxt.model.ClassGroupChat;
import com.smfx.yxt.model.Grid;
import com.smfx.yxt.model.StudentInfo;
import com.smfx.yxt.page.OrderBy;
import com.smfx.yxt.page.Page;
import com.smfx.yxt.reqVo.ClassGroupChatReqVO;
import com.smfx.yxt.service.IClassGroupChatService;
import com.smfx.yxt.service.IStudentService;
import com.smfx.yxt.utils.FileUtils;
import com.smfx.yxt.view.ClassGroupChatView;
import com.smfx.yxt.vo.GroupChatQueryVO;

/**
 * 班级群聊
 * @author huqimeng
 * @date 2017年5月25日
 */
@Controller
public class ClassGroupChatController extends BaseController{
	
	private Logger logger = LoggerFactory.getLogger(ClassGroupChatController.class);
	
	@Autowired
	private IClassGroupChatService classGroupChatService;
	@Autowired
	private IStudentService studentService;
	
	private static final String TEACHER_ONLINE = Const.TEACHER_ROLE + MenuConst.GROUPCHAT_CLASS + "/online";
	private static final String STUDENT_ONLINE = Const.STUDENT_ROLE + MenuConst.GROUPCHAT_CLASS + "/online"; 
	
	@RequestMapping("chat")
	public String chat() {
		return "chat";
	}

	/**
	 * 班级群聊 老师
	 * @param model
	 * @return
	 */
	@RequestMapping(Const.TEACHER_ROLE + MenuConst.GROUPCHAT_CLASS)
	public String teacherGroupChatClass(Model model) {
		String clazz = request.getParameter("clazz");
		if(StringUtils.isBlank(clazz)) {
			model.addAttribute("redirectUrl", Const.TEACHER_ROLE + MenuConst.GROUPCHAT_CLASS);
			return "redirect:" + Const.TEACHER_ROLE + MenuConst.USER_ROLE_URL;
		}
		String grade = request.getParameter("grade");
		if(StringUtils.isNotBlank(grade)) {
			//model.addAttribute("gradeName", getCurrentTerm().getGradeName(Integer.valueOf(grade)));
		}
		model.addAttribute("grade", grade);
		model.addAttribute("clazz", clazz);
		return "groupChat/class";
	}
	
	/**
	 * 班级群聊 家长
	 * @param model
	 * @return
	 */
	@RequestMapping(Const.STUDENT_ROLE + MenuConst.GROUPCHAT_CLASS)
	public String studentGroupChatClass(Model model) {
		AppUser appUser = getAppUser();
		//model.addAttribute("gradeName", getCurrentTerm().getGradeName(appUser.getParent().getGrade()));
		model.addAttribute("grade", appUser.getParent().getGrade());
		model.addAttribute("clazz", appUser.getParent().getClazz());
		return "groupChat/class";
	}
	
	/**
	 * 私信 老师端
	 * @param model
	 * @return
	 */
	@RequestMapping(Const.TEACHER_ROLE + MenuConst.GROUPCHAT_CLASS + "/sx")
	public String teacherPrivateLetter(Model model) {
		String sid = request.getParameter("sid");
		if(StringUtils.isBlank(sid)) return null;
		Integer studentId = Integer.valueOf(sid);
		StudentInfo student = studentService.selectById(studentId);
		model.addAttribute("grade", student.getGrade());
		model.addAttribute("clazz", student.getClazz());
		model.addAttribute("studentId", student.getId());
		model.addAttribute("studentName", student.getName());
		return "groupChat/sx";
	}
	
	/**
	 * 私信 家长端
	 * @param model
	 * @return
	 */
	@RequestMapping(Const.STUDENT_ROLE + MenuConst.GROUPCHAT_CLASS + "/sx")
	public String studentPrivateLetter(Model model) {
		String sid = request.getParameter("sid");
		if(StringUtils.isBlank(sid)) return null;
		Integer studentId = Integer.valueOf(sid);
		StudentInfo student = studentService.selectById(studentId);
		model.addAttribute("grade", student.getGrade());
		model.addAttribute("clazz", student.getClazz());
		model.addAttribute("studentId", student.getId());
		model.addAttribute("studentName", student.getName());
		return "groupChat/sx";
	}
	
	/**
	 * 分页查询相册
	 * @param vo
	 * @return
	 */
	@RequestMapping(MenuConst.GROUPCHAT_CLASS + "/page/query")
	@ResponseBody Object pageQuery(GroupChatQueryVO vo) {
		try {
			Page<ClassGroupChatView> page = new Page<ClassGroupChatView>();
			Grid grid = new Grid();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("createTime", "desc"));
			page.setSorts(sorts);
			page.setParams(vo);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			List<ClassGroupChatView> list = classGroupChatService.selectListByPage(page);
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			logger.error("--pageQuery error={}", e);
		}
		return null;
	}
	
	/**
	 * 在线群聊
	 * @param reqVO
	 * @return
	 */
	@RequestMapping(value={TEACHER_ONLINE,STUDENT_ONLINE})
	@ResponseBody Object groupChatOnline(ClassGroupChatReqVO reqVO) {
		try {
			AppUser appUser = getAppUser();
			if(appUser.getRoleType() == UserRole.PARENT.getCode()) {
				reqVO.setParentId(appUser.getParent().getId());
				reqVO.setSchoolId(appUser.getParent().getSchoolId());
				reqVO.setSchoolName(appUser.getParent().getSchoolName());
				reqVO.setStudentId(appUser.getParent().getStudentId());
			}else {
				reqVO.setTeacherId(appUser.getTeacher().getId());
				reqVO.setSchoolId(appUser.getTeacher().getSchoolId());
				reqVO.setSchoolName(appUser.getTeacher().getSchoolName());
			}
			reqVO.setTermId(getCurrentTerm().getId());
			if(reqVO.getMessageType().equals(FileType.IMAGE.getCode())) {
				String path = FileUtils.writeLocalFile(reqVO.getBaseData(), reqVO.getExtName(), RelationType.CLASS_GROUP_CHAT.getCode());
				if(path == null) {
					return new RspResult(Status.FAIL);
				}
				reqVO.setMessage(path);
			}
			ClassGroupChat record = classGroupChatService.insertSelective(reqVO);
			return new RspResult(Status.SUCCESS, record);
		} catch (Exception e) {
			logger.error("----groupChatOnline error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
}
